package com.muqi.app.pj.shops;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.flyco.banner.anim.select.RotateEnter;
import com.flyco.banner.anim.unselect.NoAnimExist;
import com.flyco.widget.BannerItem;
import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.NetWorkUtils;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.ResponseUtils;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.project.utils.LogUtil;
import com.muqi.app.project.utils.ShowToast;
import com.muqi.app.qlearn.student.BaseFragmentActivity;
import com.muqi.app.qlearn.student.R;
import com.muqi.app.user.widget.MyGridView;
import com.muqi.app.user.widget.SimpleImageBanner;
import com.muqi.app.widget.pulltorefresh2.XScrollView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

public class ShopActivity extends BaseFragmentActivity implements
		XScrollView.IXScrollViewListener, OnItemClickListener {
	private XScrollView mScrollView;
	private SimpleImageBanner topBanners;
	private MyGridView goodsView;
	private ShopAdapter mAdapter;
	private List<ShopBannerBean> bannerDatas;
	private List<ShopBannerBean> goodsLists = new ArrayList<ShopBannerBean>();;
	private int page = 1;
	private TextView tv_total;
	private TextView tv_remian;
	private TextView tv_exchange;
	private TextView tv_record;

	private MyGridView  gv_xuni;
	private FictitiousAdapter adapter;
	private List<FictitiousShopInfo> prizeInfos;
	private LinearLayout ll_shop_xuni;
	private TextView tv_more;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_shop);
	}

	@SuppressLint("InflateParams")
	@Override
	protected void onInit() {
		mScrollView = (XScrollView) findViewById(R.id.xMyScrollView);
		mScrollView.setPullRefreshEnable(true);
		mScrollView.setPullLoadEnable(false);
		mScrollView.setAutoLoadEnable(true);
		mScrollView.setIXScrollViewListener(this);
		mScrollView.setRefreshTime("刚刚");

		View content = LayoutInflater.from(this).inflate(
				R.layout.activity_shop_content, null);
		tv_total = (TextView) content.findViewById(R.id.tv_shop_total);
		tv_exchange = (TextView) content.findViewById(R.id.tv_shop_exchange);
		tv_remian = (TextView) content.findViewById(R.id.tv_shop_residue);
		gv_xuni = (MyGridView) content.findViewById(R.id.gv_xuni);
		if (content != null) {
			topBanners = (SimpleImageBanner) content
					.findViewById(R.id.home_simple_banner);
			goodsView = (MyGridView) content.findViewById(R.id.shop_gv);
			goodsView.setOnItemClickListener(this);
		}
		mScrollView.setView(content);
		
		tv_record = (TextView) findViewById(R.id.tv_shop_record);
		tv_record.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				startActivity(MineOrderListActivity.class);

			}
		});
		gv_xuni.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
			/*	 // 进入到立即兑换页面
				Intent intent=new Intent(mContext, BuyXuniGoodActivity.class);
				intent.putExtra(BuyXuniGoodActivity.GOODS_DETAILS_BEAN, prizeInfos.get(arg2));
				startActivity(intent);*/
				Intent intent = new Intent(mContext, FictiousShopDetialActivity.class);
				intent.putExtra("shop_id", prizeInfos.get(arg2).id);
				/*intent.putExtra("xuni", true);
				intent.putExtra("info", prizeInfos.get(arg2));*/
				startActivity(intent);
				
			}
		});
		ll_shop_xuni=(LinearLayout) content.findViewById(R.id.ll_shop_dummy);
		tv_more=(TextView) content.findViewById(R.id.tv_more);
		tv_more.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				startActivity(FictitiousActivity.class);
				
			}
		});
	}

	@Override
	protected void onData() {
		loadingBannerDatas();
		loadingGoodsLists();
		loadingGredit();
		loadingFictitious();
	}

	/**
	 * 加载虚拟商品
	 */
	private void loadingFictitious() {
		HashMap<String, Object> sendMap = new HashMap<String, Object>();
		sendMap.put("token", mSpUtil.getToken());
		sendMap.put("class_manager_id", mSpUtil.getChildClassId());
		String requestUrl = ParamsUtils.buildParams(
				NetWorkApi.XUNI_GOODS_LIST_API, sendMap);
		MyAsyncHttp.get(mContext, requestUrl, new AsyncCallBackListener() {

			@Override
			public void onAsyncCallBack(RESULTCODE resuleCode, String request,
					String result) {

				if (resuleCode == RESULTCODE.SUCCESS) {
					Log.e("===", "fgfh" + result);
					 prizeInfos=ResponseUtils.getFictitiousShopInfo(mContext, result);
					 if(prizeInfos!=null && prizeInfos.size()>0){
						 ll_shop_xuni.setVisibility(View.VISIBLE);
						 if(prizeInfos.size()>2){
							 tv_more.setVisibility(View.VISIBLE);
							 prizeInfos=prizeInfos.subList(0, 2);
						 }else{
							 tv_more.setVisibility(View.GONE);
						 }
						 adapter=new FictitiousAdapter(prizeInfos, mContext);
						 gv_xuni.setAdapter(adapter);
					 }else{
						 ll_shop_xuni.setVisibility(View.GONE);
					 }

				}

			}
		});

	}

	private void loadingGredit() {
		HashMap<String, Object> sendMap = new HashMap<String, Object>();
		sendMap.put("token", mSpUtil.getToken());
		sendMap.put("user_id", mSpUtil.getUserId());
		String requestUrl = ParamsUtils.buildParams(NetWorkApi.CREDIT_TOP_API,
				sendMap);
		MyAsyncHttp.get(mContext, requestUrl, new AsyncCallBackListener() {

			@Override
			public void onAsyncCallBack(RESULTCODE resuleCode, String request,
					String result) {

				if (resuleCode == RESULTCODE.SUCCESS) {
					Log.e("===", "fgfh" + result);
					try {
						JSONObject obj = new JSONObject(result);
						JSONObject dataOb = obj.getJSONObject("data");
						Log.e("===", dataOb.toString());
						if (dataOb != null) {
							tv_remian.setText(dataOb
									.getString("credits_balance"));
							tv_exchange.setText(dataOb
									.getString("credits_used"));
							tv_total.setText(dataOb.getString("credits"));

						}

					} catch (JSONException e) {

						e.printStackTrace();
					}

				}

			}
		});

	}

	public void onBack(View v) {
		this.finish();
	}

	private void loadingBannerDatas() {
		if (NetWorkUtils.isNetworkAvailable(this)) {
			MyAsyncHttp.get(this, NetWorkApi.SHOP_TOP_BANNERS_API,
					new AsyncCallBackListener() {

						@Override
						public void onAsyncCallBack(RESULTCODE resuleCode,
								String request, String result) {
							if (resuleCode == RESULTCODE.SUCCESS) {
								bannerDatas = ResponseUtils.getShopGoods(
										mContext, result);
								if (bannerDatas != null
										&& bannerDatas.size() > 0) {
									showTopBannersView();
								}
							}
						}
					});
		} else {
			ShowToast.showShort(this, R.string.net_break);// 提示无网络
		}
	}

	protected void showTopBannersView() {
		List<BannerItem> adListInfo = new ArrayList<BannerItem>();
		for (ShopBannerBean item : bannerDatas) {
			BannerItem info = null;
			info = new BannerItem();
			info.imgUrl = item.fileurl.fileurl_00;
			info.title = item.mingcheng;
			adListInfo.add(info);
		}
		topBanners.setSelectAnimClass(RotateEnter.class)
				.setUnselectAnimClass(NoAnimExist.class).setDelay(3)
				.setPeriod(3).setSource(adListInfo).startScroll();
		topBanners.setOnItemClickL(new SimpleImageBanner.OnItemClickL() {

			@Override
			public void onItemClick(int position) {

			}
		});
	}

	private void loadingGoodsLists() {
		if (NetWorkUtils.isNetworkAvailable(this)) {
			HashMap<String, Object> sendMap = new HashMap<String, Object>();
			sendMap.put("page", page);
			String requestUrl = ParamsUtils.buildParams(
					NetWorkApi.SHOP_GOODS_LIST_API, sendMap);
			MyAsyncHttp.get(this, requestUrl, new AsyncCallBackListener() {

				@Override
				public void onAsyncCallBack(RESULTCODE resuleCode,
						String request, String result) {
					mScrollView.stopRefresh();
					mScrollView.stopLoadMore();
					mScrollView.setRefreshTime("刚刚");
					if (resuleCode == RESULTCODE.SUCCESS) {
						List<ShopBannerBean> getGoods = ResponseUtils
								.getShopGoods(mContext, result);
						if (getGoods != null && getGoods.size() > 0) {
							if (page == 1) {
								goodsLists.clear();
								goodsLists = getGoods;
								if (mAdapter != null)
									mAdapter = null;
								mAdapter = new ShopAdapter(goodsLists, mContext);
								goodsView.setAdapter(mAdapter);
							} else {
								for (ShopBannerBean item : getGoods) {
									goodsLists.add(item);
								}
								mAdapter.notifyDataSetChanged();
							}

							if (getGoods.size() < 10) {
								mScrollView.setPullLoadEnable(false);
							} else {
								mScrollView.setPullLoadEnable(true);
								page++;
							}
						}
					}
				}
			});
		} else {
			ShowToast.showShort(this, R.string.net_break);// 提示无网络
		}
	}

	@Override
	public void onRefresh() {
		LogUtil.e("刷新");
		page = 1;
		loadingGoodsLists();
	}

	@Override
	public void onLoadMore() {
		LogUtil.e("加载更多");
		loadingGoodsLists();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		ShopBannerBean bean = goodsLists.get(arg2);
		if (bean != null) {
			Intent intent = new Intent(mContext, ShopDetialActivity.class);
			intent.putExtra("shop_id", bean.shangpin_id);
			startActivity(intent);
		}

	}

}
